import { transform } from '@babel/standalone'
import { evaluate } from 'eval5'

// eval5 版本
export const excuteCode = (code: string, ctx: any = {}) => {
    const es5Code = transform(code, {
        presets: ['env'],
        sourceType: 'script', // 使用 script 模式，不生成 'use strict'
    }).code
    if (!es5Code) {
        return new Error('转换代码失败')
    }

    // const es5Code = code
    const result = evaluate(es5Code, ctx, {
        ecmaVersion: 2020,
        timeout: 1000,
    })
    return result
}
